package cn.itrip.auth.controller;

import cn.itrip.auth.service.MailService;
import cn.itrip.auth.service.RegService;
import cn.itrip.auth.service.SmsService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.dao.ItripUserMapper;
import cn.itrip.utlis.DtoUtil;
import cn.itrip.utlis.MD5;
import cn.itrip.utlis.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName : cn.itrip.auth.controller.RegController
 * @Description : 类描述
 * Created by Li on 2020-12-21 15:14:08
 */
@RestController
@RequestMapping("/api")
public class RegController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private MailService mailService;
    @Autowired
    private RegService regService;

    @RequestMapping("/ckusr")
    @ResponseBody
    public Dto ckusr(String name){
        if(cunZai(name)){
            return DtoUtil.returnFail("账号存在","100001");
        }
        return DtoUtil.returnSuccess();
    }

    @Autowired
    private ItripUserMapper userMapper;

    @RequestMapping("/registerbyphone")
    public Dto registerByPhone(@RequestBody ItripUser user) {
        if(cunZai(user.getUserCode())){
            return DtoUtil.returnFail("账号存在","100001");
        }
        String code = smsService.send(user.getUserCode());
        if("error".equals(code)){
            return DtoUtil.returnFail("发送失败","100001");
        }else {
           addUser(user);
        }
        if("1".equals(code)){
            return DtoUtil.returnFail("请一分钟后再发","100001");
        }
        return DtoUtil.returnSuccess();
    }

    @RequestMapping("/doregister")
    public Dto doRegister(@RequestBody ItripUser user) {
        String code = RandomUtils.random(4);
        mailService.send(user.getUserCode(),"爱旅行验证码", code);
        addUser(user);
        return DtoUtil.returnSuccess();
    }

    @PutMapping("/validatephone")
    public Dto validatePhone(String user,String code) {
        String id = regService.selIdByUserCode(user,code);
        Pattern pattern = Pattern.compile("[0-9]*");
        if( !pattern.matcher(id).matches() ){
            return DtoUtil.returnFail(id,"100001");
        }
        ItripUser user1 = new ItripUser();
        user1.setId(Long.valueOf(id));
        user1.setFlatID(user1.getId());
        user1.setActivated(1);
        userMapper.updateByPrimaryKey(user1);
        return DtoUtil.returnSuccess();
    }

     @PutMapping("/activate")
    public Dto activate(String user,String code) {
        String id = regService.selIdByUserCode(user,code);
        Pattern pattern = Pattern.compile("[0-9]*");
        if( !pattern.matcher(id).matches() ){
            return DtoUtil.returnFail(id,"100001");
        }
        ItripUser user1 = new ItripUser();
        user1.setId(Long.valueOf(id));
        user1.setFlatID(user1.getId());
        user1.setActivated(1);
        userMapper.updateByPrimaryKey(user1);
        return DtoUtil.returnSuccess();
    }




    private int addUser(ItripUser user){
        user.setUserPassword(MD5.md5(user.getUserPassword()));
        user.setUserType(0);
        user.setActivated(0);
        return userMapper.insert(user);
    }

    private boolean cunZai(String userCode){
        if (userMapper.selectByUserCode(userCode) == null) {
            return false;
        }
        return true;
    }
}
